package com.wizo.csb;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Random;
/**
 * Basic storage that just prints out the requested operations, instead of doing them against a storage.
 * @author Wizo
 *
 */
public class BasicStorage extends Storage{
	
	public static final String VERBOSE = "basicStorage.verbose";
	public static final String VERBOSE_DEFAULT="true";
	
	public static final String SIMULATE_DELAY="basicStorage.simulatedelay";
	public static final String SIMULATE_DELAY_DEFAULT="0";
	
	Random random;
	boolean verbose;
	int delay;
	
	public BasicStorage(){
		random = new Random();
		delay = 0;
	}
	
	void delay(){
		if(delay>0){
			try{
				Thread.sleep((long)random.nextInt(delay));
			}catch(InterruptedException e){
				//do nothing
			}
		}
	}
	
	/**
	 * Initialize any state of this storage.
	 * Called once per storage instance; there is one storage instance per client thread.
	 */
	public void init(){
		verbose = Boolean.parseBoolean(getProperties().getProperty(VERBOSE, VERBOSE_DEFAULT));
		delay = Integer.parseInt(getProperties().getProperty(SIMULATE_DELAY, SIMULATE_DELAY_DEFAULT));
		
//		if(verbose){
//			System.out.println("***************** properties *****************");
//			Properties p = getProperties();
//			if(p!=null){
//				for(Enumeration e=p.propertyNames(); e.hasMoreElements(); ){
//					String name = (String)e.nextElement();
//					System.out.println(name+"=\""+p.getProperty(name)+"\"");
//				}
//			}
//			System.out.println("**********************************************");
//		}
	}
	
	/**
	 * Download an object from the bucket.
	 * 
	 * @param bucket The name of the bucket.
	 * @param name The object name of the object to downlaod.
	 * @return inputStream The inputStream of the object content. If null that means the object does't 
	 * exist or something bad happened.
	 */
	public InputStream download(String bucket, String name){
		delay();
		
		InputStream inputStream = null; 
		
		if(verbose){
			System.out.println("DOWNLOAD "+bucket+" "+name);
		}
		
		return inputStream;
	}
	
	/**
	 * Upload an object from the bucket.
	 * 
	 * @param bucket The name of the bucket.
	 * @param name The object name of the object to upload.
	 * @param inputStream The inputStream of the object content to upload.
	 * @param contentLength The length of the object content.
	 * @param contentType The type of the object content.
	 * @return Zero on success, a non-zero error code on error.
	 */
	public int upload(String bucket, String name, InputStream inputStream, 
			long contentLength, String contentType){
		delay();
		
		if(verbose){
			System.out.println("UPLOAD "+bucket+" "+name);
		}
		
		return 0;
	}
	
	/**
	 * Delete an object for good from bucket.
	 * 
	 * @param bucket The name of the bucket.
	 * @param name The object name of the object to delete.
	 * @return Zero on success, a non-zero error code in error or "not found".
	 */
	public int delete(String bucket, String name){
		delay();
		if(verbose){
			System.out.println("DELETE "+bucket+" "+name);
		}
		
		return 0;
	}
	
	/**
	 * Copy an objectS from bucketS to bucketD and rename it objectD. Replace the metadata depand on the
	 * value of replaceMetadata
	 * 
	 *  @param bucketS The source bucket.
	 *  @param objectS The source object.
	 *  @param bucketD The destination bucket.
	 *  @param objectD The destination object.
	 *  @param replaceData Ture if you want to replace metadata, otherwise you don't.
	 *  @return Zero on success, a non-zero error code on error or "not found".
	 */
	public int copy(String bucketS, String objectS, String bucketD, String objectD, 
			boolean replaceMetadata){
		delay();
		if(verbose){
			System.out.println("COPY "+objectS+"["+bucketS+"]->"+objectD+"["+bucketD+"] And replaceMetadata"
					+" is "+replaceMetadata);
		}
		
		return 0;
	}
	
	/**
	 * Move an objectS from bucketS to bucketD and rename it objectD, and objectS in bucketS will not
	 * exist any more. Replace the metadata depand on the value of replaceMetadata.
	 * 
	 *  @param bucketS The source bucket.
	 *  @param objectS The source object.
	 *  @param bucketD The destination bucket.
	 *  @param objectD The destination object.
	 *  @param replaceData Ture if you want to replace metadata, otherwise you don't.
	 *  @return Zero on success, a non-zero error code on error or "not found".
	 */
	public int move(String bucketS, String objectS, String bucketD, String objectD, 
			boolean replaceMetadata){
		delay();
		if(verbose){
			System.out.println("MOVE "+objectS+"["+bucketS+"]->"+objectD+"["+bucketD+"] And replaceMetadata"
					+" is "+replaceMetadata);
		}
		
		return 0;
	}
	
}
